home *** CD-ROM | disk | FTP | other *** search
- /*
- ** File: dsm.h
- ** Description: main header file
- **
- ** Digital Sound Machine v1.0,
- ** © 1996 Dmitry Boldyrev
- **
- */
-
- #pragma once
-
- #ifndef __SOUND__
- #include <Sound.h>
- #endif
- #include "iostream.h"
-
- #define __USE_ASM__ 1
-
- // =========================================================================================
- // Sample Mode (sample mode field)
- // =========================================================================================
-
- enum {
- SM_REVERSE = 2, // Play sample backwards flag
- SM_16BIT = 4, // 16-bit sample flag (not supported)
- SM_LOOP = 8, // Looping flag
- SM_BIDI = 16 // Ping-pong looping flag
- };
-
- // =========================================================================================
- // Playback Mode (play mode field)
- // =========================================================================================
-
- enum {
- PM_STEREO = 1, // Stereo mixing flag
- PM_INTERP = 2, // Lerping flag
- PM_SURROUND = 4, // Surround stereo flag
- PM_16BIT = 8 // 16-bit mixing flag
- };
-
- // =========================================================================================
- // Status Attributes
- // =========================================================================================
-
- enum {
- ST_ClockChanged = 1,
- ST_BPMChanged = 2,
- ST_PatChanged = 4
- };
-
- #define MAXQLEN 5
-
- class Module;
-
- // =========================================================================================
- // Mixing Freqencies
- // =========================================================================================
-
- typedef enum
- {
- mix44kHz = 44100, // 44 kHz mixing
- mix33kHz = 33075, // 33 kHz mixing
- mix22kHz = 22050, // 22 kHz mixing
- mix11kHz = 11025 // 11 kHz mixing
- } MixRate;
-
- class Mixer
- {
- public:
-
- Mixer();
- virtual ~Mixer();
-
- // =====================================================================================
- // Channel control methods
- // =====================================================================================
-
- void SetVolume (ubyte voice, ubyte volume);
- void SetFreq (ubyte voice, ulong freq);
- void SetBalance (ubyte voice, ubyte pan);
- void VoicePlay (ubyte voice, ulong start, ubyte sample);
-
- // =====================================================================================
- // Overridable methods
- // =====================================================================================
-
- virtual void PlayStart();
- virtual void PlayStop();
-
- virtual void HandleTick(void);
-
- // =====================================================================================
- // Access methods
- // =====================================================================================
-
- void SetBPM(ulong inBPM);
- ulong GetBPM(void) const;
-
- void SetMixFreq(MixRate inMixFreq);
- MixRate GetMixFreq(void) const;
-
- void SetMixChannels(ubyte inNumChannels);
- ubyte GetMixChannels(void) const;
-
- void SetMixMode(ubyte inMode);
- ubyte GetMixMode() const;
-
- bool IsActive() const;
-
- void SetGlobalVol(ubyte inVol);
- ubyte GetGlobalVol(void) const;
-
- byte* GetMixingBuffer(void) const;
- uword GetNumSamples(void) const;
- uword GetNumBytes(void) const;
-
-
- void SetInstTab(struct SAMPLE **inInstTab);
- struct SAMPLE ** GetInstTab(void) const;
-
- void UpdateMixer(void);
-
- protected:
-
- inline void DTInstall(DeferredTaskPtr dtTaskPtr);
- inline void DTInit();
- inline void DTQuit();
-
- inline void StartMixing();
- inline void StopMixing();
-
- inline uword SamplesToBytes(uword samples) const;
- inline uword BytesToSamples(uword bytes) const;
-
- inline long MixStereoNormal (byte *srce, long *dest, ulong index, ulong increment, long todo);
- inline long MixStereoInterp (byte *srce, long *dest, ulong index, ulong increment, long todo);
- inline long MixMonoNormal (byte *srce, long *dest, ulong index, ulong increment, long todo);
- inline long MixMonoInterp (byte *srce, long *dest, ulong index, ulong increment, long todo);
-
- inline void Mix32To16Surround (
- register word *dste,
- register long *srce,
- register long count);
- inline void Mix32To16Normal (
- register word *dste,
- register long *srce,
- register long count);
- inline void Mix32To8Surround (
- register byte *dste,
- register long *srce,
- register long count);
- inline void Mix32To8Normal (
- register byte *dste,
- register long *srce,
- register long count);
-
-
- inline void AddChannel (long *ptr, uword todo);
-
- void ClearMixer(void);
-
- static pascal void MyDeferredDoubleBackProc(SndChannelPtr chan, SndDoubleBufferPtr doubleBuffer);
-
- #if GENERATINGCFM
- static pascal void MyDeferredTask(struct MixerQElem* dtParam);
- #else
- static pascal void MyDeferredTask(struct MixerQElem* dtParam:__A1);
- #endif
-
- inline void WriteSamples(byte *databuf, uword size);
- inline void AdjustTicksLeft();
-
- // =====================================================================================
- // General data
- // =====================================================================================
-
- long* mLVolSel; // Left channel volume select
- long* mRVolSel; // Right channel volume select
-
- long* mStereoDelayBuf; // Stereo delay buffer
- uword mSDReadPos; // Stereo delay READ position
- uword mSDWritePos; // Stereo delay WRITE position
- uword mStereoDelay; // Stereo delay adj
-
- long* mReverbBuf; // Reverb buffer
- uword mRVReadPos; // Reverb READ position
- uword mRVWritePos; // Reverb WRITE position
- uword mReverb; // Reverb adj
-
- struct VINFO* mChanTab; // all channels
- struct VINFO* mChanRampTab; // all channels
- struct VINFO* mCurChan; // current channel
- ulong mSamples; // number of samples to mix
- long mIdxSize; // length of sample
- long mIdxLPos; // position of sample
- long mIdxLEnd; // loop end of sample
-
- long* TICKBUF; // mixing buffer
- long TICKLEFT; // number of ticks left
-
- byte mMode; // playing mode
- MixRate mMixFreq; // natural frequency
- ulong mBPM; // beats per minute
- ubyte mMixChannels; // num of channels being mixed
- ubyte mGlobalVol; // Software Global volume
- bool mActive; // true, if mixer is mixing
- long mTicksLeft; // MixFreq / BPM ratio.
- struct SAMPLE** mInstTab; // instrument table
- long mVolumeTab[65][256]; // volume table
- bool mStopPending; // Awaiting flag for synth
- long mNumFrames; // Number of frames
- ubyte mAmpShift;
- byte* mMixingData; // Pointer to current mixing buffer
-
- // =====================================================================================
- // Macintosh specific info
- // =====================================================================================
-
- static DeferredTaskUPP gDefTaskProc; // Mac deferred task desc
- static SndDoubleBackUPP gDoubleBackProc; // Mac double back task desc
-
- SndChannelPtr mSndChannel; // Mac channel sturct
- SndDoubleBufferHeader mDoubleHeader; // Mac header struct
- long mGlobA5; // Mac global A5 pointer
- QHdr mQueue; // Mac sound buffer queue
- struct MixerQElem* mEntryArr[MAXQLEN]; // Mac sound buffer queue entry
- byte mQEntries; // Number of entries in queue
- };
-
- class Module;
-
- class Synthesiser :
- public Mixer
- {
- public:
- Synthesiser(
- ubyte mode,
- MixRate mixFreq = mix22kHz,
- bool looping = false);
-
- virtual ~Synthesiser();
-
- void AttachMod(Module& mod);
-
- virtual void PlayStart();
- virtual void PlayStop(Boolean inSlideVol = false);
-
- static inline long MACfreq(uword period);
- static inline word Fine2Hz(ubyte ft);
-
- virtual void GoToNextPattern(void);
- virtual void GoToPrevPattern(void);
-
- ubyte TogglePause(void);
-
- bool IsPlaying(void) const;
- bool IsPaused(void) const;
-
- ubyte GetNumChannels(void) const;
- ubyte GetNumPatterns() const;
- ubyte GetCurPattern() const;
-
- ulong GetCurClock() const;
- bool IsDone() const;
-
- char* GetModName() const;
-
- ubyte GetStatus() const;
- void SetStatus(ubyte inStat);
-
- protected:
-
- inline void doTremor (ubyte track);
- inline void doPorta (ubyte track);
- inline void doVibrato (ubyte track);
- inline void doFineVibrato(ubyte track);
- inline void doVolslide (ubyte track);
- inline void doTremolo (ubyte track);
-
- inline void UpdateEffect (uword tick);
- void UpdateNote (void);
-
- inline void ClearChannels(void);
-
- virtual void HandleTick (void);
-
- ubyte mMasterVol; // Master volume
-
-
- struct CHANNEL* mChannel; // channels info
- struct NOTE* mCurrent; // current note pointer
-
- struct NOTE** mPatBuf; // pattern table
- ubyte* mOrder; // order table
- ubyte* mPatLen; // length of pattern table
- ubyte mNumChans; // number of channels used by song
- uword mNumOrds; // song length
- ubyte mSpeed; // song speed
-
- word mOrd; // current order
- ubyte mRow; // current row
- ubyte mPatDelay; // pattern delay flag
- ubyte mFlags; // other song flags
- ubyte mLimVol; // limiting volume
- bool mPause; // pause flag (on/off)
- bool mSongLoop; // song looping (on/off)
- bool mDone;
- ulong mClock; // current clock
-
- Module* mMod; // module link
-
- ubyte mStatus; // engine status attribute
- };
-
- // =========================================================================================
- // Module (must be overriden. basically, provides the driver io interface)
- // =========================================================================================
-
- class Module
- {
- public:
-
- Module();
- virtual ~Module();
-
- // =====================================================================================
- // Load song
- // =====================================================================================
-
- virtual error LoadSong(void);
-
- // =====================================================================================
- // Acess methods
- // =====================================================================================
-
- error getError(void) const; // Returns error code
-
- ubyte GetLimVol() const; // Returns limiting volume
-
- struct SAMPLE** GetInstTab() const; // Returns Instrument Table
- struct NOTE** GetPatTab() const; // Returns Pattern Table
- ubyte* GetOrderTab() const; // Returns Order Table
- ubyte* GetPatLenTab() const; // Returns Pattern Length Table
- ubyte* GetPanTab() const; // Returns Panning Table
-
- ubyte GetNumChans() const; // Returns Number of Channels
- uword GetNumOrds() const; // Returns Number of Orders
- ubyte GetSpeed() const; // Returns Song Speed
- ubyte GetRestartPos() const; // Returns Restart Position
- ubyte GetFlags() const; // Returns Song Flags
- ubyte GetBPM() const; // Returns Beats Per Minute ratio
- ubyte GetMasterVol() const; // Returns Master Volume
-
- char* GetName() const; // Returns Song Name
-
- protected:
-
- inline error LoadInstrument(ubyte instno, ubyte type);
-
- inline error ImportS3M(void); // Imports Scream Tracker Mod format
- inline error ImportMOD(void); // Imports Pro Tracker Mod format
- inline error ImportMTM(void); // Imports MultiTracker Mod format
- inline error Import669(void); // Imports Studio 669 Mod format
- inline error ImportFAR(void); // Imports Farandole Tracker Mod format
-
- ios* fp;
-
- SAMPLE* mInstTab[256]; // instrument table
- NOTE* mPatBuf[256]; // pattern buffer
-
- ubyte mPanTab[32]; // default pan table
- ubyte mOrder[256]; // order table
- ubyte mPatLen[256]; // pattern length table
-
- ubyte mMasterVol; // Master volume
- char mName[40]; // name of the module
-
- ubyte mNumChans; // number of channels
- uword mNumInsts; // number of instruments
- uword mNumPats; // number of patterns (unused)
- uword mNumOrds; // length of order table
-
- ubyte mRestart; // restart position
- ubyte mFlags; // song flags
- ubyte mSpeed; // song speed
- ubyte mBPM; // "beats per minute" ratio
- ubyte mLimVol; // limiting volume
-
- char* mMessage; // module message
-
- word mError; // stores the error
- };
-
- class FileMod :
- public Module
- {
- public:
- FileMod(const Str255 fName, short vRefNum);
- FileMod(const SFReply &inReply);
- FileMod(const FSSpec &inSpec);
- };
-
- class ResMod :
- public Module
- {
- public:
- ResMod(ResType rType, short rID);
- };
-
- class MemMod :
- public Module
- {
- public:
- MemMod(Ptr base, long length);
- };
-